﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts;
using Web.Framework.Mapper;

namespace CrmContacto.WebApp.App
{
    public partial class PuntoDeVentaEdit : BasePage
    {
        public PuntoDeVenta ptoDeVentaCurrent
        {
            get
            {
                return (PuntoDeVenta)ViewState["ptoDeVentaCurrent"];
            }
            set
            {
                ViewState["ptoDeVentaCurrent"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton eliminarButton = new LinkButton();
        LinkButton activarInactivarButton = new LinkButton();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton guardarButton = new LinkButton();
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            LinkButton volverButton = new LinkButton();
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.CausesValidation = false;
            volverButton.ID = "BoCommandVolver";
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);

            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            activarInactivarButton.ID = "BoCommandActivar";
            activarInactivarButton.CausesValidation = false;
            SetJavascriptForClientClickDirtyChech(activarInactivarButton);
            activarInactivarButton.Click += new EventHandler(ActivarInactivarButton_Click);
            actionButtons.Add(activarInactivarButton);

            base.LoadContextActions(actionButtons);
        }

        private void ActivarInactivarButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (ptoDeVentaCurrent.Id != 0)
                {
                    int estado = ptoDeVentaCurrent.Activo ? 0 : 1;
                    WebServiceHelper.ActivarEntidad(ptoDeVentaCurrent.GetType().Name, estado, ptoDeVentaCurrent.Id);
                    ptoDeVentaCurrent.Activo = estado == 1;
                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.EliminarPuntoDeVentaDto(ptoDeVentaCurrent.Id);
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaList + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);
                //NuevoPuntoDeVenta();
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Message.RegistroEliminado"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
            try
            {
                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        NuevoPuntoDeVenta();
                    }
                    else
                    {
                        var search = new PuntoDeVentaSearch() { PuntoDeVentaId = Convert.ToInt32(this.Request.QueryString["Id"]) };
                        search.EmpresaId = BasePage.CompanyId;
                        ptoDeVentaCurrent = WebServiceHelper.GetPuntoDeVentaDto(search).First();

                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void NuevoPuntoDeVenta()
        {
            ptoDeVentaCurrent = new PuntoDeVenta();
            ptoDeVentaCurrent.EmpresaId = BasePage.CompanyId;
            ptoDeVentaCurrent.Activo = true;
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            eliminarButton.Visible = ptoDeVentaCurrent.Id != 0;
            if (ptoDeVentaCurrent.Id != 0)
            {
                SetJavascriptForDeleteRecord(eliminarButton, ptoDeVentaCurrent.Nombre);
            }
            else
            {
                activarInactivarButton.Visible = false;
            }


            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            EntityMapper.Map(ptoDeVentaCurrent, this);
            this.lblActivo.Text = ptoDeVentaCurrent.Activo ? LocalizationProvider.Instance["EstadoCliente.Activo"] : LocalizationProvider.Instance["EstadoCliente.Inactivo"];

            if (ptoDeVentaCurrent.Id != 0)
            {
                if (ptoDeVentaCurrent.Activo)
                {
                    activarInactivarButton.Text = LocalizationProvider.Instance["Bo.Command.Inactivar"];
                }
                else
                {
                    activarInactivarButton.Text = LocalizationProvider.Instance["Bo.Command.Activar"];
                }
            }
        }

        private void SetData()
        {
            EntityMapper.Map(this, ptoDeVentaCurrent);
        }

        private void Save()
        {
            ptoDeVentaCurrent = WebServiceHelper.SavePuntoDeVentaDto(ptoDeVentaCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();

                // Limpio el cache
                ClearPuntosDeVenta(HttpContext.Current);

                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaList + string.Format("?{0}=1", MiscClass.QueryShowSaveOk), true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPuntoDeVentaList, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}